import shutil

import os

list_1 = ['A1L', 'A2L', 'A3L', 'A4L', 'A1R', 'A2R', 'A3R', 'A4R', 'B1L', 'B2L', 'B3L', 'B4L', 'B1R', 'B2R',
          'B3R', 'B4R', 'C1', 'C2', 'C3L', 'C3R', 'C4', 'D1', 'D2', 'D3L', 'D3R', 'D4', 'E1L', 'E2L', 'E1R',
          'E2R', 'F1', 'F2L', 'F2R', 'F3L', 'F3R', 'F4L', 'F4R', 'F5', 'F6', 'F7', 'G1L', 'G2L', 'G3L', 'G4L',
          'G1R', 'G2R', 'G3R', 'G4R', 'J2L', 'J4L', 'J2R', 'J4R', 'H1L', 'H2L', 'H3L', 'H4L', 'H1R', 'H2R',
          'H3R', 'H4R']


def func_rename_do(path_input):
    for root_dirs, sub_dirs, files in os.walk(path_input):
        # 仅读取时间戳  对时间戳进行正序排序
        # files = [str(x).split('.')[0] for x in files if x.endswith('dat')]  # 雷达文件
        sub_dirs = [int(str(x).split('_')[-1]) for x in sub_dirs]
        sub_dirs = sorted(sub_dirs)
        for i in range(len(sub_dirs)):
            sub_dir = sub_dirs[i]
            # print(file)
            # 进行条件筛选   以非~$'开头(解决读取文档已打开问题)；选择以file_name_0结尾的文档
            # if (file.startswith('~$') is False) and (file.lower().endswith('data.xlsx') is False):
            file = os.path.join(root_dirs, str(sub_dir))

            if i % 2 == 0:
                k = 'Z'
            else:
                k = 'F'
            new_file = root_dirs + '\\' + str(sub_dir).split('_')[-1]  # 转化为时间戳 配合sub_dir 进行使用
            new_file = root_dirs + '\\' + str(i + 1) + '_' + list_1[i] + '_' + 'N' + '_' + str(sub_dir).split('_')[-1]
            # new_file = root_dirs + '\\' + str(i + 1) + '_' + str(sub_dir).split('_')[-1]  # 转化为时间戳 配合sub_dir 进行使用

            file_dat = file.replace('detectImage', 'Data') + '.dat'
            new_file_dat = new_file.replace('detectImage', 'Data') + '.dat'

            print(file)
            print(new_file)
            # print(file_dat)
            # print(new_file_dat)
            try:  # 图片文件夹重命名
                if os.path.isdir(file):
                    os.rename(file, new_file)
                    print('1', file)
                else:
                    os.rename(file, new_file)
                    print('2', file, new_file)
            except Exception as e:
                print(e)
                pass
            # # try:  # 雷达文件夹重命名
            #     if os.path.isdir(file_dat) and 'originImage' not in file_dat:
            #         os.rename(file_dat, new_file_dat)
            #         print('1', file_dat)
            #     elif 'originImage' not in file_dat:
            #         os.rename(file_dat, new_file_dat)
            #         print('2', file_dat, new_file_dat)
            #     else:
            #         pass
            # except Exception as e:
            #     print(e)
            #     pass


def func_rename(path_inputs):
    for root_ds, sub_ds, files in os.walk(path_inputs):
        for file in sub_ds:
            if file.endswith('Image') and ('Kongsao' not in root_ds):
                file_path = os.path.join(root_ds, file)
                print(file_path)
                func_rename_do(file_path)


# 清洗掉非bmp文件
def func_del_nobmp(path_input):
    for root_dirs, sub_dirs, files in os.walk(path_input):
        for file in files:  # 进行条件筛选   以非~$'开头(解决读取文档已打开问题)；选择以file_name_0结尾的文档
            if (file.startswith('~$') is False) and (file.endswith('.bmp') is False):
                file_path = os.path.join(root_dirs, file)
                # print(file_path)
                os.remove(file_path)


# 清洗掉Data非bmp文件   注：TAI30部分文件生成多余
def func_del(path_inputs):
    for root_ds, sub_ds, files in os.walk(path_inputs):
        for file in sub_ds:
            if file.endswith('Image') and ('Kongsao' not in root_ds):
                file_path = os.path.join(root_ds, file)
                print(file_path)
                func_del_nobmp(file_path)


def main_lookandmove(path_input):
    for root_dir, sub_dir, files in os.walk(path_input):
        # 查询以data结尾的文件夹
        if ('kongsao' not in root_dir.lower()) and (
                root_dir.lower().endswith('data') or root_dir.lower().endswith('image')):
            print(root_dir, print(len(sub_dir)))
            if len(sub_dir) == 0:
                try:
                    path_move = root_dir.replace('\\Data', '')
                    print(path_move)
                except OSError as e:
                    print("Error: %s - %s." % (e.filename, e.strerror))
            else:
                print(root_dir, len(sub_dir))  # 当前目录路径


if __name__ == "__main__":
    path_inputs = r"W:\wangshuan\TAI40-II\train\2022-01-19"
    # main_lookandmove(path_inputs)  # 数据监控 删除Data为空的父文件夹
    # func_del(path_inputs)  # 清洗掉data/image 中非bmp文件
    func_rename(path_inputs)  # 对bmp文件夹进行重命名，仅读取时间戳进行排序，可能找不到文件夹（原文件夹需为时间戳）
    print('-----------进程已结束-----------')
